const winston = require('winston')
const { combine, timestamp, label, printf, prettyPrint } = winston.format
const DailyRotateFile = require('winston-daily-rotate-file')

const myFormat = printf(({ level, message, label, timestamp }) => {
    const formattedTimestamp = new Date(timestamp).toLocaleString('zh-CN', {
        timeZone: 'Asia/Shanghai',
    }) // 格式化时间
    return `[${formattedTimestamp}] [${label}] ${level}: ${message}`
})

winston.loggers.add('pdd',{
    level: 'debug',
    format: combine(label({ label: '拼多多记录' }), timestamp(), myFormat),
    transports: [
        new DailyRotateFile({
            filename: 'logs/pdd/%DATE%.log',
            datePattern: 'YYYY-MM-DD',
            zippedArchive: true,
            maxSize: '20m',
            maxFiles: '14d',
            level: 'debug'
        })
    ],
})

winston.loggers.add('xianyu',{
    level: 'debug',
    format: combine(label({ label: '闲鱼记录' }), timestamp(), myFormat),
    transports: [
        // new winston.transports.File({
        //     filename: 'logs/xianyu.log'
        // })
        new DailyRotateFile({
            filename: 'logs/xianyu/%DATE%.log',
            datePattern: 'YYYY-MM-DD',
            zippedArchive: true,
            maxSize: '20m',
            maxFiles: '14d',
            level: 'debug'
        })
    ],
})
const pddLogger = winston.loggers.get('pdd')
const xyLogger = winston.loggers.get('xianyu')
const logger = function (type,level,message){
    if(type == 'pdd'){
        return pddLogger[level](message)
    }else if(type == 'xianyu'){
        return xyLogger[level](message)
    }
}
module.exports = logger
